# coding=utf-8
#

"""
火山云的封装
"""

import time

import volcenginesdkecs
import volcenginesdkcore
from volcenginesdkcore.rest import ApiException


class HuoshanProxy(object):
    """ """
    def __init__(self):
        configuration = volcenginesdkcore.Configuration()
        configuration.ak = "AKLTZWZjZDE5ZjUwZTNkNDFiM2JiOGVlYjE2ZWFlODBmMmY"
        configuration.sk = "TXpjMU4ySmtZV1V4TnpKaE5HRTNZMkpoTWpVMk9EYzVNelk0TVdFMk1Eaw=="
        configuration.region = "cn-datong"
        # set default configuration
        volcenginesdkcore.Configuration.set_default(configuration)

        self.zone_id = "cn-datong-a"
        self.image_id = "image-yd7vyl49zsfu03sbevln"
        self.sg_id = "sg-31atr45r8zri86rnugfrkk8a1"
        self.sub_id = "subnet-31atr4nij09a86rnugfyzkbz0"

        self.instance_type_dict = {"ecs.g3al-sn1.4xlarge": 100,
                                   "ecs.g3al-sn1.8xlarge": 100,
                                   "ecs.g3al-sn1.16xlarge": 100,
                                   "ecs.g3al-sn1.24xlarge": 50,
                                   "ecs.g3al-sn1.32xlarge": 50,
                                   "ecs.g3al-sn1.48xlarge": 50}

        for instance_id, _ in self.instance_type_dict.items():
            self.cache_dict[instance_id] = 0

        self.per_count = 10

    def create_reserve_instance(self):
        """ 创建预约实例 """

        is_done = False

        network_interfaces = [volcenginesdkecs.NetworkInterfaceForRunInstancesInput(subnet_id=self.sub_id,
                                                                                    security_group_ids=[self.sg_id,])]

        while not is_done:
            for instance_type_id, count in self.instance_type_dict.items():

                start_time, end_time, release_time = generate_delivery_time()

                data = {"zone_id": self.zone_id,
                        "scheduled_instance_name": "ESI-name",
                        "start_delivery_at": start_time,
                        "end_delivery_at": end_time,
                        "image_id": self.image_id,
                        "instance_type_id": instance_type_id,
                        "instance_name": "test-ECS-name",
                        "auto_release_at": release_time,
                        "delivery_type": "Reserve",
                        "elastic_scheduled_instance_type": "Segmented",
                        "count": self.per_count,
                        "network_interfaces": network_interfaces,
                        "password": "123qweASD"}
